package com.bktech.elements;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bktech.service.ImageDownloader;
import com.bktech.stupidnews.R;

public class SuggestionListAdapter extends BaseAdapter {

	public ArrayList<SuggestionPage> listPage;
	LayoutInflater inflate;
	ImageDownloader imageDownloader;

	public SuggestionListAdapter(Context context) {
		listPage = new ArrayList<SuggestionPage>();
		inflate = LayoutInflater.from(context);
		imageDownloader = new ImageDownloader(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listPage.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listPage.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LinearLayout layout = (LinearLayout) inflate.inflate(
				R.layout.suggest_page, parent, false);
		TextView title = (TextView) layout
				.findViewById(R.id.title_suggest_page);
		TextView sub = (TextView) layout.findViewById(R.id.subscribers);

		title.setText(listPage.get(position).title);
		sub.setText(listPage.get(position).subscribers + "");
		if (listPage.get(position).iconUrl != null) {
			if (!(listPage.get(position).iconUrl).equals("")) {
				ImageView imageView = (ImageView) layout
						.findViewById(R.id.image_suggest_page);
				imageDownloader.download(listPage.get(position).iconUrl,
						imageView);
			}
		}
		return layout;
	}

	public void removeData() {
		listPage.clear();
		notifyDataSetChanged();
	}

	public void addSuggestPage(ArrayList<SuggestionPage> list) {
		listPage.clear();
		listPage = list;
		notifyDataSetChanged();
	}
}
